package com.jjwxc.jwjskandriod.activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.widget.MyDialog;
import com.jjwxc.jwjskandriod.model.HotSearchResponse;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.jjwxc.jwjskandriod.widget.TagGroupView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FindActivity extends BaseActivity {

    TagGroupView tagGroupViewOne;
    TagGroupView tagGroupViewTwo;
    LinearLayout back_ll;
    LinearLayout ll_delect;
    ImageView img_find;
    EditText ed_comment;
    TextView tv_zhankai;

    @Override
    public int getContentViewId() {
        return R.layout.activity_find;
    }

    @Override
    public void findView() {
        setNoTitle();
        tagGroupViewOne = findViewById(R.id.tgv_find_one);
        tagGroupViewTwo = findViewById(R.id.tgv_find_two);
        back_ll = findViewById(R.id.back_ll);
        tv_zhankai = findViewById(R.id.tv_zhankai);
        img_find = findViewById(R.id.img_find);
        ll_delect = findViewById(R.id.ll_delect);
        ed_comment = findViewById(R.id.ed_comment);
        MobclickAgent.onEvent(this, UMConstant.Event_Search_Box_Click);

        // 默认展开
        tagGroupViewOne.requestShowMeasure(true);
    }

    @Override
    public void setListener() {
        tv_zhankai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_zhankai.getText().equals("展开")) {
                    tagGroupViewOne.requestShowMeasure(true);
                    tv_zhankai.setText("折叠");
                } else {
                    tagGroupViewOne.requestShowMeasure(false);
                    tv_zhankai.setText("展开");
                }
            }
        });
        ll_delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyDialog(FindActivity.this, "你确定要清除搜索历史记录吗？", "确定", "取消", new MyDialog.DialogClickListener() {
                    @Override
                    public void onOkClick(Dialog dialog) {
                        List<String> list = PreUtils.getList("find_log");
                        if (list != null && list.size() > 0) {
                            PreUtils.romoveKey("find_log");
                            tagGroupViewOne.setVisibility(View.INVISIBLE);
                        }
                    }

                    @Override
                    public void onNoClick(Dialog dialog) {

                    }
                }).show();

            }
        });

        ed_comment.setOnKeyListener((view, keyCode, event) -> {
                    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                        //先隱藏鍵盤
                        ((InputMethodManager) FindActivity.this.getSystemService(INPUT_METHOD_SERVICE))
                                .hideSoftInputFromWindow(FindActivity.this.getCurrentFocus()
                                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                        find();
                    }
                    return false;
                }
        );

        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find();

                /*String find = ed_comment.getText().toString();
                Intent intent = new Intent(FindActivity.this, FindContentActivity.class);
                intent.putExtra("content", find);
                startActivity(intent);
                List<String> list = PreUtils.getList("find_log");
                if (list == null) {
                    list = new ArrayList<>();
                }
                if (list.size() >= 9) {
                    list.remove(0);
                }
                String string = list.toString();
                if (!string.contains(find)) {
                    list.add(find);
                    PreUtils.setListString("find_log", list);
                }*/

            }
        });
    }

    private void find() {
        String find = ed_comment.getText().toString();
        Intent intent = new Intent(FindActivity.this, FindContentActivity.class);
        intent.putExtra("content", find);
        startActivity(intent);
        List<String> list = PreUtils.getList("find_log");
        if (list == null) {
            list = new ArrayList<>();
        }
        if (list.size() >= 9) {
            list.remove(0);
        }
        String string = list.toString();
        if (!string.contains(find)) {
            list.add(find);
            PreUtils.setListString("find_log", list);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        List<String> list = PreUtils.getList("find_log");
        if (list != null) {
            Collections.reverse(list);
            tagGroupViewOne.setRadioButtonTags(list, R.layout.tag_item_radiobutton);
        }

        getHotSearch();
    }

    @Override
    public void afterCreate() {
        List<String> list = PreUtils.getList("find_log");
        if (list != null) {
            Collections.reverse(list);
            tagGroupViewOne.requestShowMeasure(false);
            tagGroupViewOne.setRadioButtonTags(list, R.layout.tag_item_radiobutton);
        }
        tagGroupViewOne.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (check) {
                    List<String> list = PreUtils.getList("find_log");
                    Collections.reverse(list);
                    Intent intent = new Intent(FindActivity.this, FindContentActivity.class);
                    intent.putExtra("content", list.get(tagGroupViewOne.getRadioButtonCheck() - 1));
                    startActivity(intent);
                }
            }
        });
        getHotSearch();

        tagGroupViewTwo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (check) {
                    Intent intent = new Intent(FindActivity.this, FindContentActivity.class);
                    intent.putExtra("content", mData.get(tagGroupViewTwo.getRadioButtonCheck() - 1));
                    startActivity(intent);
                    MobclickAgent.onEvent(FindActivity.this, UMConstant.Event_Search_Hot_Click, mData.get(tagGroupViewTwo.getRadioButtonCheck() - 1));
                }

            }
        });
    }

    List<String> mData = new ArrayList<>();

    public void getHotSearch() {
        check = false;
        tagGroupViewTwo.clearCheck();
        post(Url.hotSearch, null, HotSearchResponse.class, new FFNetWorkCallBack<HotSearchResponse>() {
            @Override
            public void onSuccess(HotSearchResponse response) {
                if (response.getCode() == 200) {
                    mData.clear();
                    mData.addAll(response.getData());
                    tagGroupViewTwo.requestShowMeasure(true);
                    tagGroupViewTwo.setRadioButtonTags(response.getData(), R.layout.tag_item_radiobutton, true);
                    check = true;
                }

            }

            @Override
            public boolean onFail(HotSearchResponse response) {
                return false;
            }
        });
    }

    private boolean check = false;

    @Override
    protected void onRestart() {
        super.onRestart();
        check = false;
        tagGroupViewOne.clearCheck();
        tagGroupViewTwo.clearCheck();
        check = true;
    }
}
